<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/promiseAjax.js"></script>
    <script src="./js/cookie.js"></script>
</head>
<body>
    <h1>用户修改页面</h1>
    <form action="">
        头像 
        <img src="" alt="">
        
        <input type="file" name="avatar" >     
        <br>
        用户名 <input type="text" name="username">
        <br>
        邮箱：<input type="text" name="email" >
        <br>
        <input type="submit" >
    </form>
    
</body>
<script>
    var usernameInput=document.querySelector('[name="username"]')
    var emailInput=document.querySelector('[name="email"]')
    var avatarInput=document.querySelector('[name="avatar"]')
    request({
        url:'/api/users/getUsers/'+getCookie('userid'),
        headers:{
            Authorization:localStorage.getItem('token')
        }
    }).then(res=>{
        console.log(res)
        //将username和eamil放入框中
        usernameInput.value=res.data.username
        emailInput.value=res.data.email
        // document.querySelector('img').src = res.data.avatar
        document.querySelector('img').src=res.data.avatar
    })
    //准备提交表单
    document.querySelector('form').onsubmit=function(){
        var username=usernameInput.value
        var email=emailInput.value
        console.dir(avatarInput);
        var formdata=new FormData()
        formdata.append('username',username)
        formdata.append('email',email)
        formdata.append('avatar',avatarInput.files[0])
        
        request({
            url:'/api/users/editUser/'+getCookie('userid'),
            method:'put',
            data:formdata,
            headers:{
                Authorization:localStorage.getItem('token')
            }
        }).then(res=>{
            console.log(res)
        })
        return false
    }
</script>
</html>